using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Pg.BioMedics.RatObserver.Media;

namespace Pg.BioMedics.RatObserver.MediaTester
{
    public partial class MainForm : Form
    {
        #region Delegates

        private delegate void SetBoolDelegate(bool value);
        private delegate void SetIntDelegate(int value);

        #endregion

        #region Fields

        private PlayControl _PlayControl;
        private FrameBuffer _FrameBuffer;
        private ExtractForm _ExtractForm;

        #endregion

        #region Constructors

        public MainForm()
        {
            InitializeComponent();
        }

        #endregion

        #region Event handlers 

        private void openButton_Click(object sender, EventArgs e)
        {
            if (_PlayControl != null)
            {
                _PlayControl.Stop();
                _PlayControl = null;
            }


            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    // create play controler
                    Video video = new Video(openFileDialog.FileName);
                    _PlayControl = video.Play(outputPanel);

                    // create extraction frame buffer
                    _FrameBuffer = video.Extract(10, BitmapType.RGB24);

                    if (_ExtractForm != null)
                        _ExtractForm.Close();

                    _ExtractForm = new ExtractForm(_FrameBuffer);
                    _ExtractForm.Show();


                    // assign event handlers
                    _PlayControl.OnPlay += new EventHandler(delegate(object se, EventArgs ev)
                    {
                        playButton.Enabled = false;
                        stopButton.Enabled = true;
                        pauseButton.Enabled = true;
                        forwardButton.Enabled = true;
                        backwordButton.Enabled = true;

                        progressCheckTimer.Start();
                    });

                    _PlayControl.OnPause += new EventHandler(delegate(object se, EventArgs ev)
                    {
                        playButton.Enabled = true;
                        stopButton.Enabled = true;
                        pauseButton.Enabled = false;
                        forwardButton.Enabled = true;
                        backwordButton.Enabled = true;

                        progressCheckTimer.Stop();
                    });

                    _PlayControl.OnStop += new EventHandler(delegate(object se, EventArgs ev)
                    {
                        playButton.Invoke(Delegate.CreateDelegate(typeof(SetBoolDelegate), playButton, "set_Enabled"), true);
                        stopButton.Invoke(Delegate.CreateDelegate(typeof(SetBoolDelegate), stopButton, "set_Enabled"), false);
                        pauseButton.Invoke(Delegate.CreateDelegate(typeof(SetBoolDelegate), pauseButton, "set_Enabled"), false);
                        forwardButton.Invoke(Delegate.CreateDelegate(typeof(SetBoolDelegate), forwardButton, "set_Enabled"), false);
                        backwordButton.Invoke(Delegate.CreateDelegate(typeof(SetBoolDelegate), backwordButton, "set_Enabled"), false);

                        progressCheckTimer.Stop();
                    });

                    // reenable play controls
                    playButton.Enabled = true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void playButton_Click(object sender, EventArgs e)
        {
            try
            {
                _PlayControl.Play();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void pauseButton_Click(object sender, EventArgs e)
        {
            try
            {
                _PlayControl.Pause();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void stopButton_Click(object sender, EventArgs e)
        {
            try
            {
                _PlayControl.Stop();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void forwardButton_Click(object sender, EventArgs e)
        {
            try
            {
                _PlayControl.Rewind(10);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void backwordButton_Click(object sender, EventArgs e)
        {
            try
            {
                _PlayControl.Rewind(-10);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (_PlayControl != null)
            {
                _PlayControl.Stop();
                _PlayControl.Dispose();
            }
        }

        private void progressCheckTimer_Tick(object sender, EventArgs e)
        {
            if (_PlayControl != null)
                playProgress.Value = _PlayControl.Progress;
        }

        #endregion
    } 
        
}